import ServiceApi from "f-fetch-utils/src/ServiceApi";
import {BaseApi, baseWs} from "./Config";

class TaskService {
    static getAllCompleted() {
        return TaskService.__client__.Get("/task/completed", {});
    }

    static getDownloadingStatus() {
        return TaskService.__client__.Get("/task/downloading-status", {});
    }

    static listenStatus(callback) {
        let socket = new WebSocket(baseWs + "/task-status");
        socket.onmessage = taskStatus => {
            callback(JSON.parse(taskStatus.data));
        };
        return socket;
    }

    static createTask(id, format, title, proxy) {
        return TaskService.__client__.Post("/task", {
            body: {id, format, title, proxy}
        });
    }

    static retryTask(id, format) {
        return TaskService.__client__.Put("/task/retry/{id}/{format}", {
            urlData: {
                id: id,
                format: format
            },
        });
    }

    static deleteTaskInterrupt(id, format) {
        return TaskService.__client__.Delete("/task/interrupt/{id}/{format}", {
            urlData: {
                id: id,
                format: format
            },
        });
    }

    static stopTask(id, format) {
        return TaskService.__client__.Put("/task/stop/{id}/{format}", {
            urlData: {
                id: id,
                format: format
            },
        });
    }
}

TaskService.__client__ = new ServiceApi("task-service", BaseApi);

export default TaskService;